package zozopo.warehouse;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;
import org.json.JSONArray;
import org.json.JSONException;

import zozopo.background.operator.operations.OperatorTB;
import zozopo.background.operator.operations.OperatorlimitedTB;
import zozopo.database.BusiDatabaseManager;
import zozopo.database.DatabaseManager;

import zozopo.warehouse.operations.GoodsOS;
import zozopo.warehouse.operations.GoodsTB;
import zozopo.warehouse.operations.GoodsclassOS;
import zozopo.warehouse.operations.GoodsclassTB;
import zozopo.warehouse.operations.GoodsclasslistOS;
import zozopo.warehouse.operations.GoodsclasslistTB;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class ModifyGoodsAction extends ActionSupport implements Action,SessionAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final String limitedCode ="WAREHOUSE.GOODS.MODIFY";
	
	//传入参数
	private int id_goods;
	private String name_goods = "";
	private String unit_goods = "";
	private String barcode_goods = "";
	private String url_goods = "";
	private String remark_goods = "";
	private String leveone_goods = "";
	private String levetwo_goods = ""; 
	private String image_goods = "";
	
	private double kg_goods;
	
	private String goodsclassjson;
	
	//传出参数
	private GoodsTB goods;
	
	private List<GoodsclassTB> goodsclassList;
	private LinkedHashMap<Integer,GoodsclassTB> goodsclassMap;
	
	private LinkedHashMap<Integer,Integer> goodsclasslistMap;
	
	private Map<Object, Object> session;
	public void setSession(Map arg0) {
		// TODO Auto-generated method stub
		session = arg0;
	}
	public String execute(){
		
		DatabaseManager databaseManager = new BusiDatabaseManager();
		Connection conn = databaseManager.getConnection();
		
		GoodsOS goodsOS = new GoodsOS(conn);
		GoodsclasslistOS goodsclasslistOS = new GoodsclasslistOS(conn);
		
		//获取产品类型
		GoodsclassOS goodsclassOS = new GoodsclassOS(conn);
		goodsclassList = goodsclassOS.select();
		goodsclassMap = new  LinkedHashMap<Integer,GoodsclassTB>();
		getGoodsclassMap(0);
		
		
		goods = new GoodsTB();
		goods.setId_goods(id_goods);
		goods.setName_goods(name_goods); 
		goods.setUnit_goods(unit_goods);
		goods.setBarcode_goods(barcode_goods);
		goods.setUrl_goods(url_goods);
		goods.setRemark_goods(remark_goods);
		goods.setLeveone_goods(leveone_goods);
		goods.setLevetwo_goods(levetwo_goods);
		//goods.setImage_goods(image_goods);
		goods.setKg_goods(kg_goods);
		
		
		try {
			conn.setAutoCommit(false);
			goodsclasslistOS.deleteAll(id_goods);
			
			JSONArray jsonArray = new JSONArray(goodsclassjson);
			for(int i = 0; i < jsonArray.length(); i++) {
				String goodsclassid = (String) jsonArray.get(i);
				GoodsclassTB goodsclass = goodsclassMap.get(Integer.parseInt(goodsclassid));
				
				GoodsclasslistTB goodsclasslist = new GoodsclasslistTB();
				goodsclasslist.setGoodsclassid_goodsclasslist(goodsclass.getId_goodsclass());
				goodsclasslist.setGoodsid_goodsclasslist(id_goods);
				goodsclasslist.setGoodsclassname_goodsclass(goodsclass.getName_goodsclass());
				
				if(!goodsclasslistOS.insert(goodsclasslist)){
					throw new SQLException();
				}
				
			}
			goodsOS.upadteGoods(goods);
			conn.commit();
			conn.setAutoCommit(true);
			
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			try {
				conn.rollback();
			} catch (SQLException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			e1.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			try {
				conn.rollback();
			} catch (SQLException e2) {
				// TODO Auto-generated catch block
				e2.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			goodsclasslistMap = goodsclasslistOS.getClassNGoodsidMap(id_goods);
			goods = goodsOS.getGoodsById(id_goods);
			databaseManager.releaseConnection(conn);
		}
		
		return SUCCESS;
	}
	
	public void validate() {
		//权限处理
		OperatorTB operator = (OperatorTB)session.get("operatorInfo"); 
	
		if(operator == null){
			addFieldError("errormessage",getText("operator.timeoutornotlogin"));
			return ; 
		} 
		
		//用户权限
		Map<String,OperatorlimitedTB> operatorlimitedMap = (Map<String,OperatorlimitedTB>)session.get("operatorlimitedMap");
		
		if(operatorlimitedMap.get(limitedCode) == null){
			addFieldError("errormessage","用户未有添加产品权限.");
			return;
		}
	}
	
	public LinkedHashMap<Integer,GoodsclassTB>  getGoodsclassMap(int parentid){
		
		
		for(GoodsclassTB goodsclass:goodsclassList){
			if(!goodsclassMap.containsKey(goodsclass.getId_goodsclass())){//如果已不存在
				if(goodsclass.getParentid_goodsclass() == parentid){//如果父ID正常
					goodsclassMap.put(goodsclass.getId_goodsclass(), goodsclass); 
					getGoodsclassMap(goodsclass.getId_goodsclass());
				}
			}
		}
		
		return goodsclassMap;
	}
	
	public String getName_goods() {
		return name_goods;
	}
	public void setName_goods(String name_goods) {
		this.name_goods = name_goods;
	}
	public String getUnit_goods() {
		return unit_goods;
	}
	public void setUnit_goods(String unit_goods) {
		this.unit_goods = unit_goods;
	}
	public String getBarcode_goods() {
		return barcode_goods;
	}
	public void setBarcode_goods(String barcode_goods) {
		this.barcode_goods = barcode_goods;
	}
	public String getUrl_goods() {
		return url_goods;
	}
	public void setUrl_goods(String url_goods) {
		this.url_goods = url_goods;
	}
	public String getRemark_goods() {
		return remark_goods;
	}
	public void setRemark_goods(String remark_goods) {
		this.remark_goods = remark_goods;
	}
	public String getLeveone_goods() {
		return leveone_goods;
	}
	public void setLeveone_goods(String leveone_goods) {
		this.leveone_goods = leveone_goods;
	}
	public String getLevetwo_goods() {
		return levetwo_goods;
	}
	public void setLevetwo_goods(String levetwo_goods) {
		this.levetwo_goods = levetwo_goods;
	}
	public GoodsTB getGoods() {
		return goods;
	}
	public void setGoods(GoodsTB goods) {
		this.goods = goods;
	}
	public int getId_goods() {
		return id_goods;
	}
	public void setId_goods(int id_goods) {
		this.id_goods = id_goods;
	}
	public String getImage_goods() {
		return image_goods;
	}
	public void setImage_goods(String image_goods) {
		this.image_goods = image_goods;
	}
	public static String getLimitedcode() {
		return limitedCode;
	}
	public double getKg_goods() {
		return kg_goods;
	}
	public void setKg_goods(double kg_goods) {
		this.kg_goods = kg_goods;
	}
	public String getGoodsclassjson() {
		return goodsclassjson;
	}
	public void setGoodsclassjson(String goodsclassjson) {
		this.goodsclassjson = goodsclassjson;
	}
	public List<GoodsclassTB> getGoodsclassList() {
		return goodsclassList;
	}
	public void setGoodsclassList(List<GoodsclassTB> goodsclassList) {
		this.goodsclassList = goodsclassList;
	}
	public LinkedHashMap<Integer, GoodsclassTB> getGoodsclassMap() {
		return goodsclassMap;
	}
	public void setGoodsclassMap(LinkedHashMap<Integer, GoodsclassTB> goodsclassMap) {
		this.goodsclassMap = goodsclassMap;
	}
	public LinkedHashMap<Integer, Integer> getGoodsclasslistMap() {
		return goodsclasslistMap;
	}
	public void setGoodsclasslistMap(
			LinkedHashMap<Integer, Integer> goodsclasslistMap) {
		this.goodsclasslistMap = goodsclasslistMap;
	}
	

}
